package com.guigu.qingcheng.controller;


import com.guigu.qingcheng.domain.pojo.TbBrand;
import com.guigu.qingcheng.domain.entity.PageResult;
import com.guigu.qingcheng.service.TbBrandService;
import com.guigu.qingcheng.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/qingcheng/brand")
@CrossOrigin
public class TbBrandController {

    @Autowired
    private TbBrandService brandService;

    // 查询所有数据
    @GetMapping("/findAll")
    public CommonResult findAll(){
        List<TbBrand> list = brandService.list(null);
        if (list==null){
            return CommonResult.error();
        }
        System.out.println("11122");
        System.out.println("hha");
        System.out.println("俺是你爸爸");
        return CommonResult.ok().data("brandList",list);
    }

    // 分页查询
    @GetMapping("/findPage")
    public PageResult<TbBrand> findPage(Integer page, Integer size) {
        return brandService.findPage(page, size);
    }

    // 分页条件查询
    @PostMapping("/findPage")
        public CommonResult findPage(@RequestBody Map<String, Object> searchMap, Integer page, Integer size) {
        PageResult<TbBrand> brandPageResult = brandService.findPage(searchMap, page, size);
        if (brandPageResult.getRows()==null && brandPageResult.getTotal() == 0){
            return CommonResult.error();
        }
        return CommonResult.ok().data("brandRows",brandPageResult.getRows()).data("total",brandPageResult.getTotal());
    }

    @PostMapping("/insert")
    public CommonResult insert(@RequestBody TbBrand brand) {
        boolean save = brandService.save(brand);
        if (save){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody TbBrand brand) {
        boolean update = brandService.updateById(brand);
        if (update) {
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    @GetMapping("/delete")
    public CommonResult delete(Integer id) {
        boolean remove = brandService.removeById(id);
        if (remove){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        TbBrand brand = brandService.getById(id);
        if (brand==null){
            return CommonResult.error();
        }
        return CommonResult.ok().data("brand",brand);
    }

}

